package com.asia5b.a5mall.middleware.logic.viewmodel.order

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.event.CartDataChange
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.utils.getEventBus

import com.asia5b.a5mall.middleware.utils.showMessage
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/1/13.
 */
class OrderDetailActivityVM:SimpleViewModel() {

    val getOrderDetailResultLiveData= MutableLiveData<OrderDetailModel>()
    val rogResultLiveData= MutableLiveData<Boolean>()
    val cancalResultLiveData= MutableLiveData<Boolean>()
    val getTradeSerialNoResultLiveData= MutableLiveData<String>()
    val cancelRefundResultLiveData= MutableLiveData<String>()

    fun getOrderDetail(orderSn: String) {
        val observable = orderApi.getOrderDetail(orderSn)
        baseSubscribe(observable, Consumer { orderDetailModel ->
//            providerView().getOrderDetailResult(orderDetailModel)
            getOrderDetailResultLiveData.value=orderDetailModel
        })
    }


    fun cancel(orderSn: String, reson: String) {
        baseSubscribe(orderApi.cancel(orderSn, reson), Consumer { result ->
            showMessage(R.string.取消成功)
//            providerView().cancelResult()
            cancalResultLiveData.value=true
        })
    }

    fun rog(orderSn: String) {
        baseSubscribe(orderApi.rog(orderSn), Consumer { result ->
            showMessage(R.string.确认收货成功)
//            providerView().rogResult()
            rogResultLiveData.value=true
        })
    }

    fun addCart(skuId: Int) {
        baseSubscribe(cartApi.add(skuId, 1, null).map { 1 }, Consumer {
            getEventBus().post(CartDataChange())
            showMessage(R.string.加入购物车成功)
        })
    }

    fun getTradeSerialNo(order_sn: String) {
        val observable = tradeApi.getTradeSerialNo(order_sn)
                .map { tradeSerialNoResult ->
                    tradeSerialNoResult.string()
                }
        baseSubscribe(observable, Consumer {
//            providerView().getTradeSerialNoResult(it)
            getTradeSerialNoResultLiveData.value=it
        })
    }
}